package com.jy.vod.service.impl;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.jy.vod.service.VodService;
import com.jy.vod.utils.ConstantVodUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;

@Service
public class VodServiceImpl implements VodService {
    @Override
    public String uploadVideoAly(MultipartFile file) {
        try {
            //accessKeyId,accessKeySecret
            //fileName  上传文件原始名称
            String fileName = file.getOriginalFilename();
            //title   上传之后显示的名称
            String title = fileName.substring(0,fileName.lastIndexOf("."));
            //inputStream 上传文件输入流，可通过file.get方法调用到
            InputStream inputStream = file.getInputStream();
            UploadStreamRequest request =
                    new UploadStreamRequest(ConstantVodUtils.ACCESS_KEY_ID,ConstantVodUtils.ACCESS_KEY_SECRET,
                            title, fileName, inputStream);
            UploadVideoImpl uploader = new UploadVideoImpl();
            UploadStreamResponse response = uploader.uploadStream(request);
            String videoId = null;
//        System.out.println("RequestId"+response.getRequestId()+"\n"); //请求视频点播服务的请求ID
            if(response.isSuccess()){
                videoId=response.getVideoId();
//            System.out.println("VideoId="+response.getVideoId()+"\n");
            }else{ //如果设置回调URL无效，不影响视频上传，可以返回VideoId同时返回错误码，其他情况上传失败，VideoId为空，此时需要根据返回信息
                videoId=response.getVideoId();
//            System.out.println("VideoId="+response.getVideoId()+"\n");
//            System.out.println("ErrorCode="+response.getCode()+"\n");
//            System.out.println("ErrorMessage "+response.getMessage()+"\n");
            }
            return videoId;
        }catch (Exception e){
            return null;
        }

    }
}
